Skip to content

Parse scientific notation and mixed integer/decimal IN lists in row filters - #4371

Open
rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/parser-sci-notation-in-lists
Open

rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/parser-sci-notation-in-lists

Conversation

@rahulsmahadev

@rahulsmahadev rahulsmahadev commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

The row filter parser rejects two kinds of valid numeric literals:

tbl.scan(row_filter="price > 1e-3")
# ParseException: Expected end of text, found 'e'  (at char 9), (line:1, col:10)
tbl.scan(row_filter="quantity in (1, 2.5)")
# ParseException: Expected ')', found '.'  (at char 17), (line:1, col:18)

Decimal literals used pyparsing's common.real(), which has no exponent, so 1e-3 parses as 1 and the rest is left over. A literal set had to be all decimals or all integers, so a list that mixes 1 and 2.5 did not parse either.

This change parses a number with an exponent (1e-3, -1.5E3, 2.5e1) as a DoubleLiteral: in ANSI SQL it is an approximate numeric literal, and Spark, Trino and DuckDB parse it as a double. Numbers without an exponent parse as before (DecimalLiteral or LongLiteral). A literal set can now mix integers, decimals and doubles. Each token is parsed to its own literal type before the existing set deduplication and binding rules apply. To keep long_col = 1e3 working, a DoubleLiteral with a whole value now converts to int and long columns; a fractional value raises, as it does for a DecimalLiteral. Binding to decimal and double columns uses the existing conversions (a double is rounded to the scale of a decimal column, and a value outside the column range binds to a sentinel). A list that mixes numbers with strings still raises a ParseException (test_in_different_types).

Are these changes tested?

Yes. In tests/expressions/test_parser.py, test_scientific_notation covers exponents on either side of a comparison and in BETWEEN, test_in_mixed_numbers covers mixed IN lists (in both orders) and NOT IN lists, test_bind_numeric_literals binds exponent forms against decimal, long and double columns, and a mixed integer and double IN list against a long column, and test_bind_out_of_range_numeric_literals covers values outside a decimal or long column range (dec = 1e10, dec < 1e30, lng = 1e19), a value that rounds up to the column maximum, and IN lists that contain an out-of-range value. In tests/expressions/test_literals.py, test_double_to_integer_and_long_conversion covers the new conversion, and test_invalid_float_conversions no longer expects a DoubleLiteral to reject int and long. The new tests fail on main and pass with this change.

Are there any user-facing changes?

Bug fix: row filter strings accept scientific notation (as doubles) and IN lists that mix numeric literals. A DoubleLiteral with a whole value now converts to int and long. A value outside the range of a decimal column binds to a constant (dec = 1e10 is AlwaysFalse) instead of a value that does not fit the column or an InvalidOperation error, and an IN list leaves out such values, so lng in (1e19, 7) matches only 7. Expressions that parsed before parse to the same result.

This pull request and its description were written by Isaac.

The row filter parser only accepted decimals with a fractional part and
literal sets of a single numeric kind, so `x > 1e-3` and `x in (1, 2.5)`
raised a ParseException.

Co-authored-by: Isaac <no-reply@databricks.com>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@jackylee-ch could you take a look when you have a chance? This small change to the expression parser lets 1e-3 and IN (1, 2.5) parse. You last updated this parser in #3917.

@Fokko

Fokko commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Thanks for picking this up! I think the scientific notation part should follow ANSI SQL. In ANSI, <mantissa>E<exponent> is an approximate numeric literal (so a double), while only digits with an optional decimal point are exact numeric (decimal). Spark 3+, Trino and DuckDB all parse 1e-3 as a double.

Parsing it as a DecimalLiteral also causes a binding problem: Decimal("1e3") is 1E+3 with scale -3, so dec_col = 1e3 cannot bind to any decimal column, and price > 1e-3 only binds when price has exactly scale 3. The tests don't catch this because Decimal("2E+2") == Decimal("200").

Could we emit a DoubleLiteral for the exponent forms instead? That would need a DoubleLiteral.to(LongType) conversion so long_col = 1e3 keeps working. It would also be great to add a few tests that bind against a schema with decimal, long and double columns.

The mixed integer/decimal IN list change looks good to me and is in line with ANSI.

…er types

A number with an exponent is an approximate numeric literal in ANSI SQL, and
Spark, Trino and DuckDB parse 1e-3 as a double. As a DecimalLiteral, 1e3 has
scale -3 and could not bind to decimal columns. Parse the exponent forms as
DoubleLiteral instead, and convert a whole DoubleLiteral to int and long columns
(a fractional value raises, as for decimals) so that long_col = 1e3 binds.

Co-authored-by: Isaac <no-reply@databricks.com>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

Thanks, done in bccbed0: numbers with an exponent now parse as DoubleLiteral and the others as before, and a whole DoubleLiteral converts to long (and int, so int_col = 1e3 keeps working); a fractional one raises like a DecimalLiteral. test_bind_numeric_literals binds them against decimal, long and double columns, so dec_col = 1e3 now binds as 1000.00. I also updated the description.

@jackylee-ch jackylee-ch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update; the double-literal approach looks right to me. One question inline.

)
for expression, expected in [
("dec = 1e3", DecimalLiteral(Decimal("1000.00"))),
("dec > 2.5e-1", DecimalLiteral(Decimal("0.25"))),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dec <= 5e-3 binds to dec <= 0.01 (and dec = 1e-3 to dec = 0.00) because DoubleLiteral.to(DecimalType) rounds half-up to the column scale, so a 0.01 row matches a 0.005 bound. Should exponent forms on a decimal column fail on a scale mismatch like DecimalLiteral does, and can this case get a test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jackylee-ch, good catch. DoubleLiteral.to(DecimalType) rounds half-up to the column scale, like Java's DoubleLiteral.to(DECIMAL), so the expression API already binds LessThanOrEqual("dec", 0.005) as dec <= 0.01; this PR makes that reachable from string filters. I'd suggest raising Could not convert 0.005 into a decimal(10, 2) when the value does not fit the column scale exactly, so dec <= 5e-3 fails like dec <= 0.005 while dec <= 5e-1 still binds as 0.50. That also changes the expression API for Python floats on decimal columns and differs from Java's rounding, so I'd like to confirm that's acceptable before changing it. I'll add a test either way.

Comment thread pyiceberg/expressions/literals.py Outdated
@@ -445,6 +445,16 @@ def _(self, type_var: DecimalType) -> Literal[Decimal]:
raise ValueError(f"Could not convert {self.value} into a {type_var}, value is not finite")
return DecimalLiteral(Decimal(self.value).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Large values are a problem here, and filter strings can now reach them:

  • dec = 1e30 on a decimal(9, 2) column raises decimal.InvalidOperation, because quantize fails once the result needs more than 28 digits (the default context precision).
  • dec = 1e10 binds to 10000000000.00, which does not fit in decimal(9, 2).

Could this return an above max / below min literal instead, like the int and long conversions do? The existing bind logic then turns dec = 1e30 into AlwaysFalse and dec < 1e30 into AlwaysTrue. There is no decimal sentinel yet, so this needs a DecimalAboveMax and DecimalBelowMin. Something like this before the quantize:

limit = Decimal(10) ** (type_var.precision - type_var.scale)
value = Decimal(self.value)
if value >= limit:
    return DecimalAboveMax()
elif value <= -limit:
    return DecimalBelowMin()

And the same check once more after the quantize, since rounding can carry 9999999.996 to 10000000.00.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in fd3da9c. Out-of-range values now return DecimalAboveMax or DecimalBelowMin, checked before and after the quantize, so dec = 1e30 binds to AlwaysFalse and dec < 1e30 to AlwaysTrue. The quantize runs in a context sized for the column precision. Set predicates also leave out sentinels, since they match no row. Before, lng in (1e19, 7) also returned the maximum long.

for expression, expected in [
("dec = 1e3", DecimalLiteral(Decimal("1000.00"))),
("dec > 2.5e-1", DecimalLiteral(Decimal("0.25"))),
("dec = 12.34", DecimalLiteral(Decimal("12.34"))),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add cases for values outside the column range, such as dec = 1e10, dec < 1e30, dec > -1e30 and lng = 1e19, asserting that they bind to AlwaysFalse() or AlwaysTrue()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test_bind_out_of_range_numeric_literals in tests/expressions/test_parser.py, with dec = 1e10, dec < 1e30, dec > -1e30, lng = 1e19, the 9999999.996 rounding case and the IN cases. dec = 1e10 binds to 10000000000.00 on base, so the test fails there.

rahulsmahadev and others added 2 commits November 30, 2026 00:15
DoubleLiteral and FloatLiteral quantized the value before checking the column range, so 1e30 raised
InvalidOperation and 1e10 bound to 10000000000.00 on decimal(9, 2). The conversion now returns
DecimalAboveMax or DecimalBelowMin, as the int and long conversions do, and checks the range again
after rounding, since 9999999.996 carries up to 10000000.00.

Set predicates leave out literals that bind to a sentinel, because they match no row. Without this,
IN with an out-of-range value bound the sentinel value itself, so lng in (1e19, 7) matched the maximum
long.

Co-authored-by: Isaac <no-reply@databricks.com>
Keep the shortest-repr rounding from main in the decimal conversion, with the sentinel checks from this
branch, and keep main's error labels with the double and mixed-list alternatives from this branch.

Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants